package cn.xiaopengstack.domain.agent.service.execute.flow.factory;

import cn.bugstack.wrench.design.framework.tree.StrategyHandler;
import cn.xiaopengstack.domain.agent.model.entity.ExecuteCommandEntity;
import cn.xiaopengstack.domain.agent.model.valobj.AiAgentClientFlowConfigVO;
import cn.xiaopengstack.domain.agent.service.execute.flow.node.RootNode;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangyangang
 */
@Service
public class DefaultFlowAgentExecuteStrategyFactory {

    @Resource(name = "flowExecuteRootNode")
    private RootNode rootNode;

    public StrategyHandler<ExecuteCommandEntity, DynamicContext, String> getAgentStrategyHandler(){
        return rootNode;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class DynamicContext{

        public static final String INITIALIZING = "INITIALIZING";
        public static final String ANALYZING_TOOLS = "ANALYZING_TOOLS";
        public static final String PLANNING = "PLANNING";
        public static final String PARSING_STEPS = "PARSING_STEPS";
        public static final String EXECUTING_STEPS = "EXECUTING_STEPS";
        public static final String COMPLETED = "COMPLETED";

        public enum Key{
            /**
             * AI对任务当前任务需要执行的MCP工具分析的结果
             */
            mcpAnalysisResult,
            /**
             * AI对当前任务的执行计划
             */
            planningResult,
            ;
        }

        /**
         * 任务执行阶段及结果
         */
        private Map<String, String> stepsMap = new HashMap<>();

        /**
         * 执行状态
         */
        private String status;

        /**
         * 任务执行结束时间
         */
        private Long endTime = 0L;

        /**
         * 任务开始时间
         */
        private Long startTime = 0L;

        /**
         * 任务执行步骤
         */
        private int step = 0;

        /**
         * 执行历史
         */
        private StringBuilder executionHistory;

        /**
         * 当前任务
         */
        private String currentTask;

        /**
         * 任务是否完成
         */
        boolean isCompleted = false;
        /**
         * client的配置
         */
        private Map<String, AiAgentClientFlowConfigVO> aiAgentClientFlowConfigVOMap;

        /**
         * 字典
         */
        private Map<String, Object> dataObjects = new HashMap<>();

        /**
         * 流式响应体
         */
        private ResponseBodyEmitter emitter;

        public <T> void setValue(String key, T value) {
            dataObjects.put(key, value);
        }

        public <T> T getValue(String key) {
            return (T) dataObjects.get(key);
        }

        /**
         * 获取任务执行的总时间
         * @return
         */
        public Long getExecuteTotalTime(){
            return startTime - endTime;
        }

        /**
         * 步骤递增
         */
        public void incrStep(){
            this.step += 1;
        }
    }
}
